package com.eureka.journal.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eureka.bclass.base.BasePageResult;
import com.eureka.bclass.base.BaseResult;
import com.eureka.bclass.util.StrUtil;
import com.eureka.journal.entity.MailEntity;
import com.eureka.journal.service.MailService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  邮件 前端控制器
 * </p>
 *
 * @author lixing_java@163.com
 * @since 2021-06-06
 */
@RestController
@RequestMapping("/mail")
public class MailController {
    @Resource
    private MailService mailService;

    /**
     * @Description 邮件搜索条件
     * @Author lixing
     * @Date 2021/6/6 10:53
     */
    public static QueryWrapper<MailEntity> queryWrapperForEmail(MailEntity entity) {
        QueryWrapper<MailEntity> queryWrapper = new QueryWrapper<>();
        if (entity.baseCreateTimeStart != null) {
            queryWrapper.ge("create_time", entity.baseCreateTimeStart);
        }
        if (entity.baseCreateTimeEnd != null) {
            queryWrapper.le("create_time", entity.baseCreateTimeEnd);
        }
        //        if (entity.getCreateUserGrade() != null) {
        //            if (entity.getCreateUserGrade() == 2) {
        //                queryWrapper.in("create_user_grade", 2, 3, 4);
        //            } else if (entity.getCreateUserGrade() == 3) {
        //                queryWrapper.in("create_user_grade", 3, 4);
        //            } else if (entity.getCreateUserGrade() == 4) {
        //                queryWrapper.eq("create_user_grade", 4);
        //            }
        //            if (StrUtil.isEffective(entity.getCreateUserAccount())) {
        //                queryWrapper.eq("createUserAccount", entity.getCreateUserAccount());
        //            }
        //        }
        if (entity.getCategory() != null) {
            queryWrapper.eq("category", entity.getCategory());
        }
        if (StrUtil.isEffective(entity.getReceiver())) {
            queryWrapper.eq("receiver", entity.getReceiver());
        }
        if (StrUtil.isEffective(entity.getText())) {
            queryWrapper.likeLeft("text", entity.getText());
        }
        return queryWrapper;
    }

    /*
     * @Description TODO [邮件]分页列表
     * @Author lixing
     * @Date 2019/2/16 13:08
     */
    @PostMapping("pageMail")
    public BasePageResult<MailEntity> pageMail(@RequestBody MailEntity entity) {
        Page<MailEntity> page = mailService.page(new Page<>(entity.basePageNum, entity.basePageSize), queryWrapperForEmail(entity));
        return new BasePageResult<>(page);
    }

    /*
     * @Description TODO [邮件]列表
     * @Author lixing
     * @Date 2019/9/11 9:14
     */
    @PostMapping("listMail")
    public BaseResult<List<MailEntity>> listMail(@RequestBody MailEntity entity) {
        List<MailEntity> mailEntityList = mailService.list(queryWrapperForEmail(entity));
        return new BaseResult<List<MailEntity>>().success(mailEntityList);
    }

    /*
     * @Description TODO [邮件]添加
     * @Author lixing
     * @Date 2019/2/16 13:08
     */
    @PostMapping("insertMail")
    public BaseResult<String> insertMail(@RequestBody MailEntity entity) throws Exception {
        return null;
    }

    /*
     * @Description TODO [邮件]删除
     * @Author lixing
     * @Date 2019/9/11 9:14
     */
    @PostMapping("deleteMail")
    public BaseResult<Boolean> deleteMail(@RequestBody MailEntity entity) {
        boolean flag;
        if (!entity.getBaseLongList().isEmpty()) {
            flag = mailService.removeByIds(entity.getBaseLongList());
        } else if (null != entity.getId()) {
            flag = mailService.removeById(entity.getId());
        } else {
            return new BaseResult<Boolean>().fail("主键缺失");
        }
        return flag ? new BaseResult<Boolean>().success() : new BaseResult<Boolean>().fail();
    }

    /*
     * @Description TODO [邮件]详情
     * @Author lixing
     * @Date 2019/9/11 9:14
     */
    @PostMapping("detailMail")
    public BaseResult<MailEntity> detailMail(@RequestBody MailEntity entity) {
        entity = mailService.getById(entity.getId());
        return new BaseResult<MailEntity>().success(entity);
    }
}
